(*
This file is part of the Lustre reader

* Copyright (c) 2010, Nicola Camuffo.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of Nicola Camuffo, nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)


open Data_printer
open Exn_printer
open Exceptions
open Lustables
open Envs


let start fn = (

  let use_file = open_in fn in
  let lexbuf = Lexing.from_channel use_file in

  try

	  let pid = idcounter#next in
    open_new_env pid;

    Lusparser.main Luslexer.token lexbuf;


    let flags = [Open_creat;Open_trunc;Open_wronly] in
    let grant = 0o777 in

    let base = Filename.basename fn in
    let name = Filename.chop_suffix base ".lus" in
    let tabsfile = Filename.current_dir_name ^ "/" ^ name ^ ".tables" in

    let hndl = (open_out_gen flags grant tabsfile) in

    (* prints tables and syntax trees *)
		print_data hndl;

    flush hndl

  with
    Parsing.Parse_error ->
			Printf.printf "\nParse error at line %d (col %d): '%s'\n" 
				(!Filepos.linenum) 
				((Lexing.lexeme_start lexbuf) - !Filepos.linepos)
				(Lexing.lexeme lexbuf);
			exit 2
    | exn ->
			print_exception exn;
			exit 2
		
		
(*		
		Type_mismatch_exception s ->
      Printf.printf "\nType mismatch exception: '%s'\n" s;
      exit 2
    | Utils_exception s ->
      Printf.printf "\nUtils exception: '%s'\n" s;
      exit 2


    | Lusparser.Parse_mismatch_exception s ->
      Printf.printf "\nParse_mismatch_exception: '%s'\n" s;
      exit 2
*)
)

(*
  let hndl = (open_out_gen flags grant (basefile ^ ".tree")) in
  let print_assignments h =
    let print h (x,e) = output_string h (data#get_varname x); output_string h "\n"; print_expression h "" e in

    let nids = data#get_all_nodeids () in
    List.iter (fun nid ->
      output_string h ("<< "^(data#get_nodename nid)^" >>\n");
      
      let l = data#get_assigns nid in
      List.iter (fun x -> print h x; output_string h "\n") l;

      output_string h "\n\n"
    ) nids
  in

  print_assignments hndl;
  flush hndl
*)


let _ =
  Arg.parse [] start ""
